x<template>
  <a-form class="smart-query-form" v-privilege="'blog:query'">
    <a-row class="smart-query-form-row">
      <a-form-item label="关键字" class="smart-query-form-item" name="title">
        <a-input style="width: 300px" v-model:value="queryForm.title" placeholder="标题" />
      </a-form-item>
      <a-form-item label="分类" name="type" class="smart-query-form-item">
        <DictSelectType :service-id="SERVICE_ID_ENUM.DICTIONARY.value" :dcit-type="DICT_TYPE_ENUM.BLOG_TYPE.value" v-model:value="queryForm.type" width="120px" />
      </a-form-item>
      <a-form-item label="推荐级别" name="recommendLevel" class="smart-query-form-item">
        <DictSelectType :service-id="SERVICE_ID_ENUM.DICTIONARY.value" :dcit-type="DICT_TYPE_ENUM.BLOG_RECOMMEND_LEVEL.value" v-model:value="queryForm.recommendLevel" width="120px" />
      </a-form-item>
      <a-form-item label="是否发布" class="smart-query-form-itme" name="isPublish">
        <a-switch v-model:checked="queryForm.isPublish" checkedValue="Y" unCheckedValue="N"/>
      </a-form-item>
      <a-form-item label="是否原创" class="smart-query-form-itme" name="isOriginal">
        <a-switch v-model:checked="queryForm.isOriginal" checkedValue="Y" unCheckedValue="N"/>
      </a-form-item>
      <a-form-item label="开启评论" class="smart-query-form-itme" name="openComment">
        <a-switch v-model:checked="queryForm.openComment" checkedValue="Y" unCheckedValue="N"/>
      </a-form-item>
      <a-form-item label="是否推广" class="smart-query-form-itme" name="isPublicize">
        <a-switch v-model:checked="queryForm.isPublicize" checkedValue="Y" unCheckedValue="N"/>
      </a-form-item>
      <a-form-item class="smart-query-form-item smart-margin-left10">
        <a-button-group>
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="onReload">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-button-group>
      </a-form-item>
    </a-row>
  </a-form>

  <a-card size="small" :bordered="false">
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-button type="primary" size="small" @click="addOrUpdate()" v-privilege="'blog:add'">
          <template #icon>
            <PlusOutlined />
          </template>
          新建
        </a-button>
      </div>
    </a-row>

    <a-table
      rowKey="id"
      :columns="tableColumns"
      :scroll="{ x: 1000 }"
      :dataSource="tableData"
      :pagination="false"
      :loading="tableLoading"
      size="small"
      bordered
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'title'">
          <router-link tag="a" target="_blank" :to="{ path: '/help-doc/detail', query: { docId: record.id } }">{{
            record.title
          }}</router-link>
        </template>
        <template v-else-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button type="link" @click="addOrUpdate(record.id)" v-privilege="'blog:update'">编辑</a-button>
            <a-button type="link" danger @click="onDelete(record.id)" v-privilege="'blog:delete'">删除</a-button>
          </div>
        </template>
      </template>
    </a-table>

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageIndex"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryBlogList"
        @showSizeChange="queryBlogList"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>

  <BlogFoemModel ref="blogFoemModelRef" @reloadList="queryBlogList" />
</template>

<script setup lang="ts">
  import { message, Modal } from 'ant-design-vue';
  import { onMounted, reactive, ref, watch } from 'vue';
  import BlogFoemModel from './blog-form-model.vue';
  import { blogApi } from '/@/api/blog-api/blog-api';
  import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { smartSentry } from '/@/lib/smart-sentry';
  import { SERVICE_ID_ENUM,DICT_TYPE_ENUM } from '/@/constants/system/cache-service-id-const';
  import DictSelectType from '/@/components/cache/dict-select-type.vue';

  const props = defineProps({
    // 目录id
    helpDocCatalogId: Number,
  });

  const queryFormState = {
    title: '', //标题
    type: '', //分类
    recommendLevel: '', //推荐级别
    isPublish: undefined, 
    isOriginal: undefined, 
    openComment: undefined, 
    isPublicize: undefined, 
    pageIndex: 1,
    pageSize: PAGE_SIZE,
  };
  const queryForm = reactive({ ...queryFormState });

  const tableColumns = ref([
    {
      title: `标题`,
      dataIndex: 'title',
      ellipsis: true,
    },
    {
      title: '作者',
      dataIndex: 'createBy',
      width: 120,
      ellipsis: true,
    },
    {
      title: `是否原创`,
      dataIndex: 'isOriginalStr',
      width: 80,
      ellipsis: true,
    },
    {
      title: '分类',
      dataIndex: 'typeStr',
      width: 90,
    },
    {
      title: '标签',
      dataIndex: 'labelName',
      width: 90,
    },
    {
      title: '推荐级别',
      dataIndex: 'recommendLevelStr',
      width: 90,
    },
    {
      title: '点击量',
      dataIndex: 'clickCount',
      width: 150,
    },
    {
      title: '开启评论',
      dataIndex: 'openCommentStr',
      width: 90,
    },
    {
      title: '发布状态',
      dataIndex: 'isPublishStr',
      width: 150,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 150,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 90,
    },
  ]);

  // ------------------ 查询相关 ------------------

  const tableData = ref([]);
  const total = ref(0);
  const tableLoading = ref(false);

  onMounted(() => {
    queryBlogList();
  });

  // 查询列表
  async function queryBlogList() {
    try {
      tableLoading.value = true;
      const result = await blogApi.searchPage(queryForm);
      tableData.value = result.data.data;
      total.value = result.data.total;
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      tableLoading.value = false;
    }
  }

  // 点击查询
  function onSearch() {
    queryForm.pageNum = 1;
    queryBlogList();
  }

  // 点击重置
  function onReload() {
    Object.assign(queryForm, queryFormState);
    queryBlogList();
  }

  // ------------------ 新建、编辑 ------------------

  // 新建、编辑
  const blogFoemModelRef = ref();
  function addOrUpdate(docId) {
    blogFoemModelRef.value.showModal(docId);
  }

  // ------------------ 删除 ------------------

  // 删除
  function onDelete(docId) {
    Modal.confirm({
      title: '提示',
      content: '确认删除此数据吗?',
      onOk() {
        deleteHelpDoc(docId);
      },
    });
  }

  // 删除API
  async function deleteHelpDoc(docId) {
    try {
      tableLoading.value = true;
      await blogApi.delete(docId);
      message.success('删除成功');
      queryBlogList();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      tableLoading.value = false;
    }
  }
</script>

<style lang="less" scoped></style>
